<template>  
  <div>

  <!--  搜索表单-->
    <div style="margin-bottom: 20px">
      <el-input style="width: 240px" placeholder="请输入名称" v-model="params.name"></el-input>
      <el-input style="width: 240px; margin-left: 5px" placeholder="请输入联系方式" v-model="params.phone"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load">
        <i class="el-icon-search"></i> 搜索</el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset">
        <i class="el-icon-refresh"></i>重置
      </el-button>
    </div>

  <!-- 表格 -->
    <el-table :data="tableData" stripe>
      <el-table-column prop="username" label="用户名" width="200"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="sex" label="性别"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="phone" label="联系方式" width="120"></el-table-column>
      <el-table-column label="状态">
        <template v-slot="scope">
          <el-switch
              v-model="scope.row.status"
              @change="changeStatus(scope.row)"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="updatetime" label="更新时间"></el-table-column>
      <el-table-column prop="account" label="账户积分"></el-table-column>
      <el-table-column label="操作" width="220">
          <template v-slot="scope">
             <el-button size="mini" type="warning" @click="handleAccountAdd(scope.row)">充值</el-button>
             <el-button type="primary" @click="$router.push('/editUser?id=' + scope.row.id)" size="mini">编辑</el-button>
             <el-popconfirm title="您确认删除这行数据吗？" @confirm="del(scope.row.id)" style="margin-left: 5px">    
               <el-button type="danger" slot="reference" size="mini">删除</el-button>
             </el-popconfirm>
          </template>
      </el-table-column>
    </el-table>

    <!--  分页-->
    <div style="margin-top: 20px">
      <el-pagination
          background
          :page-size="5"
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :total="0">
      </el-pagination>
    </div>

    <!-- 充值小窗口 -->
    <el-dialog title="充值" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="100px" ref="ruleForm" :rules="rules" style="width: 85%">
        <el-form-item label="当前账户积分" prop="account">
          <el-input disabled v-model="form.account" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="积分" prop="score">
          <el-input v-model="form.score" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAccount">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import request from '@/utils/request';

  export default {
    name: 'UserList',
    data() {
      return {
        tableData: [],
        total: 0,
        params: {
          pageNum: 1,
          pageSize: 10,
          name: '',
          phone: ''
        },
        dialogFormVisible: false,
        form: {},
        // rules: {
        //   score: [
        //   { required: true, message: '请输入积分', trigger: 'blur'},
        //   { validator: checkNums, trigger: 'blur'}
        //   ]
        // }
      }
    },
    created() {
      this.load()
    },
    methods: {
      load() {
        // fetch('http://localhost:9090/user/list').then(res => res.json()).then(res => {
        //   this.tableData = res
        // })
        request.get('/user/page', { params: this.params }).then(res => {
          //console.log(res);
          if(res.code === '200'){
              this.tableData = res.data.list
              this.total = res.data.total
          }
        })
      },
      reset() {
        this.params = {
          pageNum: 1,
          pageSize: 10,
          name: '',
          phone: ''
        }
      },
      handleCurrentChange(pageNum){ //点击分页触发分页
        this.params.pageNum = pageNum
        this.load()
      },
      del(id) {
          request.delete("/user/delete/"+id).then(res => {
              if(res.code === '200'){
                  this.$notify.success('删除成功')
                  this.load()
              }else{
                  this.$notify.error(res.msg)
              }
          })
      },
      handleAccountAdd(row) {
        this.form = JSON.parse(JSON.stringify(row))
        this.dialogFormVisible = true
      },
      addAccount() {
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            request.post('/user/account', this.form).then(res => {
              if (res.code === '200') {
                this.$notify.success('充值成功')
                this.dialogFormVisible = false
                this.load()
              } else {
                this.$notify.error(res.msg)
              }
            })
          }
        })
      },
       changeStatus(row) {
          request.put('/user/update', row).then(res => {
            if (res.code === '200') {
              this.$notify.success('操作成功')
              this.load()
            } else {
              this.$notify.error(res.msg)
            }
          })
        } 
    }
  }
</script>